
namespace SharpTalk.Client.WpfApplication.Converters
{
    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Diagnostics;

    [ValueConversion(typeof(string), typeof(string))]
    [ValueConversion(typeof(string[]), typeof(string[]))]
    public sealed class SourceLevelsToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            if(value is string[])
            {
                var array = (string[]) value;
                for (int i = 0; i < array.Length; i++)
                    array[i] = (string)Convert(array[i], typeof (string), null, culture);

                return array;
            }

            if (value as string == null) return value;

            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.ActivityTracing),StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_activity;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.All), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_all;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Critical), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_critical;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Error), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_error;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Information), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_info;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Off), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_off;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Verbose), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_verbose;
            if (string.Equals((string)value, Enum.GetName(typeof(SourceLevels), SourceLevels.Warning), StringComparison.InvariantCultureIgnoreCase)) return Resources.Messages.trace_level_warning;
            
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value as string == null) return null;

            if (string.Equals((string)value, Resources.Messages.trace_level_activity, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.ActivityTracing);
            if (string.Equals((string)value, Resources.Messages.trace_level_all, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.All);
            if (string.Equals((string)value, Resources.Messages.trace_level_critical, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Critical);
            if (string.Equals((string)value, Resources.Messages.trace_level_error, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Error);
            if (string.Equals((string)value, Resources.Messages.trace_level_info, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Information);
            if (string.Equals((string)value, Resources.Messages.trace_level_off, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Off);
            if (string.Equals((string)value, Resources.Messages.trace_level_verbose, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Verbose);
            if (string.Equals((string)value, Resources.Messages.trace_level_warning, StringComparison.InvariantCultureIgnoreCase)) return Enum.GetName(typeof(SourceLevels), SourceLevels.Warning);

            return value;
        }
    }
}